package service.impl;

import common.Constants.ServiceConstant;
import common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import service.AuthorityService;
import service.TokenService;
import service.VerifyService;

/**
 * Created by lxl on 2015/11/3.
 */
public class VerifyServiceImpl implements VerifyService {

    @Autowired
    private TokenService tokenService ;

    @Autowired
    private AuthorityService authorityService ;

    @Override
    public Result verify(String token, int authority) {

        Result result = new Result() ;

        if( tokenService.verifyToken(token)  == ServiceConstant.VERIFY_SUCCES ){

            result.setError(String.valueOf(authorityService.getAuthority(tokenService.getUserName(token))));

            if( authority == ServiceConstant.NO_LEVEL ){

                result.setSucces(true);

            }
            else if ( authority == authorityService.getAuthority(tokenService.getUserName(token))){

                result.setSucces(true);

            }else {

                result.setSucces(false);
                result.setError(ServiceConstant.VERIFY_AUTHORITY_ERROR);

            }

        }else {

            result.setSucces(false);
            result.setError(ServiceConstant.VERIFY_TOKEN_ERROR);

        }


        return result;
    }
}
